<template>
  <div class="curvePriceDiv">
    <el-form-item  :label="labelName" size="small" v-model="calcPricingParam">
      <el-row style="display: inline-flex">
        <el-col :span="4">
          <dictCombox v-model="calcPricingParam.curveType" :clearable="false" dictType="curveType"/>
        </el-col>

        <el-col :span="20" v-show="calcPricingParam.curveType=='fix'">
          <el-col :span="12">
            <Numberinput v-model="calcPricingParam.rate" append="%" precision="3" :min="props.min" format="#,##0.####"/>
          </el-col>
          <el-col :span="12">
            <dictCombox v-model="calcPricingParam.dayCount" prop='disDayCount' :label="xxxx" dictType="dayCounter"/>
          </el-col>
        </el-col>

        <el-col :span="20" v-show="calcPricingParam.curveType!='fix'">
          <baseCombox :url="props.url" clearable
                      prop="disIrcCode"
                      size="small" @change="props.getRateByCode()"
                      :style="{width: '100%'}" v-model="calcPricingParam.code"></baseCombox>
        </el-col>
      </el-row>
    </el-form-item>
    <!--   插值结果   -->
    <el-row v-if="calcPricingParam.curveType!='fix'">
      <el-col :span="24" >
        <el-form-item :label=" '插值结果'" >
          <Numberinput disabled  :value="getRate()" append="%" precision="3" :min="''" format="#,##0.####"/>
        </el-form-item>
      </el-col>
    </el-row>
  </div>

</template>

<script setup expose name="curvePriceCombox">

import {reactive, ref, watch, toRefs, getCurrentInstance, inject} from 'vue'
import Numberinput from '@/xquant/base/number'
import baseCombox from '@/xquant/base/baseCombox'
import dictCombox from '@/xquant/base/dictCombox'
import InputAmount from '@/xquant/base/InputAmount'
import {getTerminationPaymentDate,getFixDate,calcCurveRateBase64,calcDivCurveRateBase64,calcVolSurfaceRateBase64} from "@/api/calc/otcOption/calc";
import {isNotVoid} from "@/utils/util";

const {proxy} = getCurrentInstance()
const props = defineProps({
  url: {
    type: String,
    default: '/pricing/comboBox/queryIrcurveFilter.action'
  },
  index: {
    type: String,
    default: ''
  },
  type: {
    type: String,
    default: 'ircurve'
  },
  labelName: {
    type: String,
    default: '无风险利率'
  },
  calcPricingParam: {
    type: Object,
    default: {}
  }
  ,
  getRateByCode:{
    type:Function
  },
  min:{
    type: Number,
    default: 0
  }
})
const {url, labelName} = toRefs(props)
const calcPricingParam = reactive({
  rate: '0',
  code: '',
  rateByCode: 0,
  curveType: 'fix',
  dayCount: 'Actual/365'
})
const setCalcPricingParam = (value) => {
  /*先清空*/
    calcPricingParam.rate= '0',
    calcPricingParam.code= '',
    calcPricingParam.rateByCode= 0,
    calcPricingParam.curveType= 'fix',
    calcPricingParam.dayCount= 'Actual/365'
  if (Boolean(value)) {
    if (!Boolean(value.rate) && !Boolean(value.code)) {
      calcPricingParam.curveType = 'fix'
      calcPricingParam.rate = ''
      calcPricingParam.dayCount = ''
      return
    }
    if (Boolean(value.rate == '' || value.code)) {
      calcPricingParam.curveType = 'float'
      calcPricingParam.code = value.code
    } else {
      calcPricingParam.curveType = 'fix'
      calcPricingParam.rate = proxy.lsh.multiply(value.rate, 100)
      calcPricingParam.dayCount = value.dayCount
    }
  }
}
const getCalcPricingParam = (calcType) => {
  if (calcPricingParam.curveType == 'fix') {
    return {
      rate: proxy.lsh.round(proxy.lsh.divide(Number(calcPricingParam.rate), 100), 5),
      dayCount: calcPricingParam.dayCount,
      annualDays: parseInt(String(calcPricingParam.dayCount?.split('/')[1]))
    }
  }
  else {
    if (props.type = 'volsurface'){
      if (calcType=="invert"){
        let indexRef = Boolean(getIndexRef(props.index)[0])? getIndexRef(props.index)[0]:getIndexRef(props.index)
        if (indexRef.proxy){
          let form = indexRef.getFormData()
          if (!Boolean(form.data.strike)){
            return {
              code: calcPricingParam.code,
              // dayCount: calcPricingParam.dayCount
            }
          }
        }
      }

        return {
          rate: proxy.lsh.divide(Number(calcPricingParam.rateByCode), 100),
          dayCount: calcPricingParam.dayCount,
          annualDays: parseInt(String(calcPricingParam.dayCount?.split('/')[1]))
        }
      }

  }/*else {
    return {
      code: calcPricingParam.code,
      // dayCount: calcPricingParam.dayCount
    }
  }*/
}

const getIndexRef = inject('getIndexRef')
const getCalcPricingRateByCode = (type) => {
  if (calcPricingParam.curveType!='fix'){
    let indexRef = Boolean(getIndexRef(props.index)[0])? getIndexRef(props.index)[0]:getIndexRef(props.index)
    let endDate;
    if (indexRef.proxy){
      let form = indexRef.getFormData()
      let calcPriceParam =indexRef.getCalcPriceParam()
      // 观察日
       endDate = form.data.terminationDate;
      if ("rangeaccrual"== String(form.data.productType).toLocaleLowerCase()) {
        endDate = form.data.endDate
      }else if ("singlesharkfin" == String(form.data.productType).toLocaleLowerCase()){
        if ("American" == form.data.exerciseType) {
          endDate = form.data.endDate
        }
      }
      switch (type){
        case  'volsurface':{
          /*
          *
          * 香草期权		行权价
          * 二元期权/双层二元		行权价
          * 三层阶梯		    标的价格
          * 单鲨期权-欧式		标的价格
          * 单鲨期权-美式		标的价格
          * */
          getTerminationPaymentDate({
            'calDay': endDate
          }).then(res=>{
            if (res.errorCode>=0&&res.result){
              let endDateByCalc = res.result.CAL_DAY
              let param = {
                type : type,
                valueDate: indexRef.proxy.valueDate,
                InterpolationDate: endDateByCalc,
                code: calcPricingParam.code,
                strike: ["Vanilla","Digital","SingleSharkFin"].includes(form.data.productType)? form.data.strike :form.data.spotPrice,
                spot: calcPriceParam.spotPrice
              }
              if (["RangeAccrual","MultiDigital","DoubleDigital"].includes(form.data.productType)) {
                param.strike =  calcPriceParam.spotPrice
              }
              calcVolSurfaceRateBase64(param).then(res=>{
                let reps = res.result
                if (isNotVoid(reps)){
                  //  设置daycounter
                  calcPricingParam.dayCount = reps.daycounter
                  // calcPricingParam.annualDays = parseInt(String(reps.daycounter?.split('/')[1]))
                  //设置rate
                  calcPricingParam.rateByCode = 100 * reps.result
                }else {
                  calcPricingParam.rateByCode = 0
                }
              })
            }else {
              calcPricingParam.rateByCode = 0
            }
          })
        };break;
        case 'divyield':{
          getFixDate({
            'calDay': endDate
          }).then(res=>{
            if (res.errorCode>=0&&res.result){
              let endDateByCalc = res.result.CAL_DAY
              let param = {
                type : type,
                valueDate: indexRef.proxy.valueDate,
                PeriodsOrDates: endDateByCalc,
                code: calcPricingParam.code
              }
              calcDivCurveRateBase64(param).then(res=>{
                let reps = res.result
                //  设置daycounter
                if (isNotVoid(reps)){
                  calcPricingParam.dayCount = reps.daycounter
                  calcPricingParam.rateByCode = reps.result?100 * reps.result.zeroRates.double[0]:0
                }else {
                  calcPricingParam.rateByCode = 0
                }
              })
            } else {
              calcPricingParam.rateByCode = 0
            }
          })
        };break;
        case 'ircurve':{
          getFixDate({
            'calDay': endDate
          }).then(res=>{
            if (res.errorCode>=0&&res.result){
              let endDateByCalc = res.result.CAL_DAY
              let param = {
                type : type,
                valueDate: indexRef.proxy.valueDate,
                PeriodsOrDates: endDateByCalc,
                code: calcPricingParam.code
              }
              calcCurveRateBase64(param).then(res=>{
                let reps = res.result
                if (isNotVoid(reps)){
                  //  设置daycounter
                  calcPricingParam.dayCount = reps.daycounter
                  calcPricingParam.rateByCode = reps.result?100 * reps.result.zeroRates.double[0]:0
                }else {
                  calcPricingParam.rateByCode = 0
                }
              })
            } else {
              calcPricingParam.rateByCode = 0
            }
          })
        };break;
      }
      // getTerminationPaymentDate({
      //   'calDay': endDate
      // }).then(res=>{
      //   if (res.errorCode>=0&&res.result){
      //     let endDateByCalc = res.result.CAL_DAY
      //     switch (type){
      //       case  'volsurface':{
      //         /*
      //         *
      //         * 香草期权		行权价
      //         * 二元期权/双层二元		行权价
      //         * 三层阶梯		    标的价格
      //         * 单鲨期权-欧式		标的价格
      //         * 单鲨期权-美式		标的价格
      //         * */
      //         let param = {
      //           type : type,
      //           valueDate: indexRef.proxy.valueDate,
      //           InterpolationDate: endDateByCalc,
      //           code: calcPricingParam.code,
      //           strike: ["Vanilla","Digital","DoubleDigital","SingleSharkFin"].includes(form.data.productType)? form.data.spotPrice * form.data.strikePercentage/100 :form.data.spotPrice,
      //           spot: calcPriceParam.spotPrice
      //         }
      //         calcVolSurfaceRateBase64(param).then(res=>{
      //           let reps = res.result
      //           if (isNotVoid(reps)){
      //             //  设置daycounter
      //             calcPricingParam.dayCount = reps.daycounter
      //             // calcPricingParam.annualDays = parseInt(String(reps.daycounter?.split('/')[1]))
      //             //设置rate
      //             calcPricingParam.rateByCode = 100 * reps.result
      //           }else {
      //             calcPricingParam.rateByCode = 0
      //           }
      //         })
      //       };break;
      //       case 'divyield':{
      //         let param = {
      //           type : type,
      //           valueDate: indexRef.proxy.valueDate,
      //           PeriodsOrDates: endDateByCalc,
      //           code: calcPricingParam.code
      //         }
      //         calcDivCurveRateBase64(param).then(res=>{
      //           let reps = res.result
      //           //  设置daycounter
      //           if (isNotVoid(reps)){
      //             calcPricingParam.dayCount = reps.daycounter
      //             calcPricingParam.rateByCode = reps.result?100 * reps.result.zeroRates.double[0]:0
      //           }else {
      //             calcPricingParam.rateByCode = 0
      //           }


      //         })
      //       };break;
      //       case 'ircurve':{
      //         let param = {
      //           type : type,
      //           valueDate: indexRef.proxy.valueDate,
      //           PeriodsOrDates: endDateByCalc,
      //           code: calcPricingParam.code
      //         }
      //         calcCurveRateBase64(param).then(res=>{
      //           let reps = res.result
      //           if (isNotVoid(reps)){
      //             //  设置daycounter
      //             calcPricingParam.dayCount = reps.daycounter
      //             calcPricingParam.rateByCode = reps.result?100 * reps.result.zeroRates.double[0]:0
      //           }else {
      //             calcPricingParam.rateByCode = 0
      //           }
      //         })
      //       };break;
      //     }
      //   }
      //   else {
      //     calcPricingParam.rateByCode = 0
      //   }
      // })

    }
  }

}
const getRate = () => {
  return proxy.lsh.round(calcPricingParam.rateByCode,2)
}
//指定 ref  暴露断点
defineExpose({
  calcPricingParam,
  setCalcPricingParam, getCalcPricingParam,getCalcPricingRateByCode
})
</script>
<style scoped>
.curvePriceDiv{
  /*background-color: #F0F5F0*/
}
</style>
